package com.example.rabbitmqdemo.listener;

import com.example.rabbitmqdemo.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-04-03
 * Time: 21:13
 */
@Component
public class RetryListener {
    @RabbitListener(queues = Constants.RETRY_QUEUE)
    public void handlerMessage(Message message) throws UnsupportedEncodingException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println(message + "接收到消息" +  new String(message.getBody(),"UTF-8") + ", deliveryTag: " + deliveryTag);
        int num = 3 / 0;
        System.out.println("业务处理完成!");
    }


    /*@RabbitListener(queues = Constants.RETRY_QUEUE)
    public void handlerMessage(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println(message + "接收到消息" +  new String(message.getBody(),"UTF-8") + ", deliveryTag: " + deliveryTag);
        try{
            //int num = 3 / 0;
            System.out.println("业务处理完成!");
            //不进行批量处理
            channel.basicAck(deliveryTag,false);
        }catch (Exception e) {
            channel.basicNack(deliveryTag,false,true);
        }
    }*/
}
